package com.tf_cut.admin.seller.controller;

import com.alibaba.fastjson.JSON;
import com.tf_cut.admin.backend.api.BackEndApi;
import com.tf_cut.admin.common.pojo.PageInfo;
import com.tf_cut.admin.common.pojo.Result;
import com.tf_cut.admin.seller.vo.SellerAndAccountVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


@RestController
@RequestMapping("/seller/info")
@Slf4j
public class SellerInfoController {

    @Autowired
    private BackEndApi sellerInfoApi;

    @GetMapping("list")
    public ModelAndView sellerList() {
        return new ModelAndView("seller/info/list", "sellerInfo", null);
    }

    @GetMapping("addServiceType")
    public ModelAndView sellerServiceType() {
        return new ModelAndView("seller/info/addServiceType", "sellerInfo", null);
    }

    @PostMapping("list/serviceType")
    public Result<List<String>> sellerServiceTypes() {
        String json = sellerInfoApi.querySellerServiceType();
        return Result.of(JSON.parseObject(json, List.class));
    }

    @PostMapping("list/page")
    public Result<PageInfo<SellerAndAccountVo>> page(SellerAndAccountVo sellerAndAccountVo) {
        String json = JSON.toJSONString(sellerAndAccountVo);
        json = sellerInfoApi.querySellerInfoList(json);
        return Result.of(JSON.parseObject(json, PageInfo.class));
    }

    @PostMapping("dealSellerInfo")
    public Result<SellerAndAccountVo> dealSellerInfo(SellerAndAccountVo avo) {
        String rtnJson;
        Result<SellerAndAccountVo> result;
        if(StringUtils.isNoneBlank(avo.getIsAuthentication())) {
            try {
                rtnJson = sellerInfoApi.updateStatus(JSON.toJSONString(avo));
                result = JSON.parseObject(rtnJson, Result.class);
            } catch (Exception e) {
                log.error(e.getMessage(),e);
                avo.setSellerId(null);
                result = Result.of(avo);
                result.setFlag(false);
            }
        }else {
            Result<SellerAndAccountVo> result2 = Result.of(null);
            result2.setMsg("操作参数必传！");
            result2.setFlag(false);
            return result2;
        }
        return result;
    }

}
